<?php ob_start();
session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
		$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../../menu.php"); // redirection en cas d'echec
}
require_once('../../../Connections/microassur.php');
require_once('../../../utils/fonctions.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1"))
$bool=verifLibelle1('Code_profil') && verifLibelle('Libelle_profil');

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1") && $bool && !existeChamp($database_microassur, $microassur,'profil','Code_profil',@$_POST['Code_profil'])) {
  $insertSQL = sprintf("INSERT INTO profil (Identifiant_profil, Code_profil, Libelle_profil) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['Identifiant_profil'], "int"),
                       GetSQLValueString($_POST['Code_profil'], "text"),
                       GetSQLValueString($_POST['Libelle_profil'], "text"));

  mysql_select_db($database_microassur, $microassur);
  mysql_query("START TRANSACTION");
  $Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());
  
  if($Result1){ 
  mysql_query("commit"); header("location:index.php?ajout=success");}

else {mysql_query("rollback");$message = colorer("Contacter la DSI, erreur création profil","false");}
}
elseif(isset($_POST["MM_insert"])&& !$bool)
{

if(empty($_POST['Code_profil'])) $message = colorer("Vous devez saisir le type ","false");
elseif(!verifLibelle1('Code_profil')) $message = colorer("Le type saisi comporte des caractères spéciaux interdits ","false");
elseif(empty($_POST['Libelle_profil'])) $message = colorer("Vous devez saisir le libellé ","false");
elseif(!verifLibelle('Libelle_profil')) $message = colorer("Le libellé saisi comporte des caractères spéciaux interdits ","false");
else
//$message=colorer("Erreur: Au moins un champ est vide ou valeur incorrecte","false");
$message="";

}
if(isset($_POST["MM_insert"]) && existeChamp($database_microassur, $microassur,'profil','Code_profil',@$_POST['Code_profil']))$message = colorer("Ce profil existe déjà ","false"); 

mysql_select_db($database_microassur, $microassur);
$query_rs = "SELECT * FROM profil";
$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs = mysql_num_rows($rs);
?>
<?php  if(isset($_GET['ajout']) && $_GET['ajout']=='success') $message = colorer("Le profil est créé","true"); ?>
<?php  if(isset($_GET['supp']) && $_GET['supp']=='err') $message = colorer("Contacter la DSI, erreur suppression profil","false"); ?>
<?php if(isset($_GET['mod']) && $_GET['mod']=='success') $message = colorer("La modification du profil est prise en compte ","true"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
			<link rel="stylesheet" type="text/css" href="../hab.css.css" />
			<!--[if IE 7]><link rel="stylesheet" type="text/css" href="../../../css/ie7.css" /><![endif]-->
	</head>
<body>
			<div id="sf_menu1">
				<h3>Administration</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Profil";
					$nommodule2="Liste des profils";
					$nom_module_saisie="Ecran saisie";
					?>
			</div>
			<div class="cleared"></div>
		<div class="corps1">
			<!--/***************************************/-->
			<div class="imgleft"></div>
			<div class="imgmiddle">
			<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
			<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
			<div class="cleared"></div>
			<table class="tab_list ns0">
						<tr id="niv1">
							<td class="t1">TYPE</td>
							<td>LIBELLE</td>
						</tr>
			</table>
				<div class="content_table">
					<table class="tab_list niv2">
						  <?php $i=1; do 	{
							   if ($totalRows_rs > 0) { // Show if recordset not empty ?>
							  <tr <?php if($i%2==0) echo 'class="colorligne"'; ?> >
								<td class="t1"><a href="modprofil.php?Identifiant_profil=<?php echo $row_rs['Identifiant_profil']; ?>"><?php echo $row_rs['Code_profil']; ?></a></td>
								<td><a href="modprofil.php?Identifiant_profil=<?php echo $row_rs['Identifiant_profil']; ?>"><?php echo $row_rs['Libelle_profil']; ?></a></td>
							  </tr>
								<?php } // Show if recordset not empty ?>

							  <?php $i++; } while ($row_rs = mysql_fetch_assoc($rs)); ?>
					</table>
				</div>
			</div>
			<div class="imgright"></div>
		<div class="cleared"></div>
		<!--/***************************************/-->
		<div id="br"></div>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1"  class="hab">
			<div class="imgleft"></div>
			<div class="imgmiddle">
				<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
				<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nom_module_saisie; ?></h5></span><span class="btnright"></span></div>
					<div class="content_tableform">
							  <table>
								<tr>
								  <td class="d1">TYPE</td>
								  <td><input class="resize_1" type="text" maxlength="5" name="Code_profil" value="<?php if(isset($_POST['Code_profil']))echo remind_text('Code_profil');?>" size="32" /></td>
								</tr>
								<tr>
								  <td class="d1">LIBELLE</td>
								  <td><input name="Libelle_profil" type="text" value="<?php if(isset($_POST['Libelle_profil']))echo remind_text('Libelle_profil');?>" maxlength="30" charwidth="40" /></td>
								</tr>
							  </table>
							</div>
					</div>
					<div class="imgright"></div>				
						<div class="cleared"></div>
						
							<div class="bt1"><a href="<?php echo $retour; ?>"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
							<div class="bt"><span class="btleft"></span><input type="submit" name="Creer" value="Créer" class="resizeall btmiddle"/><span class="btright"></span></div>
							
						<div class="cleared"></div>
				<input type="hidden" name="Identifiant_profil" value="" />
				<input type="hidden" name="MM_insert" value="form1" />
			</form>
		</div>
</body>
</html><?php ob_end_flush();?>
<?php
mysql_free_result($rs);
?>